package camelinaction.service;


import camelinaction.*;
import camelinaction.dao.Player;
import camelinaction.dao.PlayerJpaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * 玩家服务
 * 
 * @author -琴兽-
 * 
 */
@Component
public class PlayerServiceImpl implements PlayerService {

	@Autowired
	private PlayerJpaRepository playerJpaRepository;

	@Override
	public PlayerResponse registerAndLogin(ChatSession session, String playerName, String passward) {
		Optional<Player> existplayer=playerJpaRepository.findByPlayerName(playerName).stream().findAny();
		if(existplayer.isPresent()){
			// 玩家名已被占用
			//if (existplayer != null) {
				throw new ErrorCodeException(ResultCode.PLAYER_EXIST);
			//}


		}else {
			// 创建新帐号
			Player player = new Player();
			player.setPlayerName(playerName);
			player.setPassward(passward);
			player = playerJpaRepository.save(player);
			//顺便登录
			return login(session, playerName, passward);
		}
	}

	@Override
	public PlayerResponse login(ChatSession session, String playerName, String passward) {
		// 判断当前会话是否已登录
		if (session.getAttachment() != null) {
			throw new ErrorCodeException(ResultCode.HAS_LOGIN);
		}
		// 玩家不存在
		Optional<Player> optionalPlayer = playerJpaRepository.findByPlayerName(playerName).stream().findAny();
		if (!optionalPlayer.isPresent()) {
			throw new ErrorCodeException(ResultCode.PLAYER_NO_EXIST);
		}
		Player player=optionalPlayer.get();
		// 密码错误
		if (!player.getPassward().equals(passward)) {
			throw new ErrorCodeException(ResultCode.PASSWARD_ERROR);
		}


		// 判断是否在其他地方登录过
		boolean onlinePlayer = SessionManager.isOnlinePlayer(player.getPlayerId());
		if (onlinePlayer) {
			ChatSession oldSession = SessionManager.removeSession(player.getPlayerId());
			oldSession.removeAttachment();
			// 踢下线
			oldSession.close();
		}

		// 加入在线玩家会话
		if (SessionManager.putSession(player.getPlayerId(), session)) {
			session.setAttachment(player);
		} else {
			throw new ErrorCodeException(ResultCode.LOGIN_FAIL);
		}

		// 创建Response传输对象返回
		PlayerResponse playerResponse = new PlayerResponse();
		playerResponse.setPlayerId(player.getPlayerId());
		playerResponse.setPlayerName(player.getPlayerName());
		playerResponse.setLevel(player.getLevel());
		playerResponse.setExp(player.getExp());
		return playerResponse;
	}
}
